package com.dense.kuiniu.bookborrow.business.home.query;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.base.BaseFragment;
import com.dense.kuiniu.bookborrow.business.entity.Book;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/8/7
 */
public class BookDetailDelegate extends BaseFragment {
    private static final String KEY = "book";

    public static BookDetailDelegate getInstance(Book book) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY, book);
        BookDetailDelegate delegate = new BookDetailDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_author)
    TextView mTvAuthor;
    @BindView(R.id.tv_binding)
    TextView mTvBinding;
    @BindView(R.id.tv_publish_time)
    TextView mTvPublishTime;
    @BindView(R.id.tv_page)
    TextView mTvPage;
    @BindView(R.id.tv_isbn)
    TextView mTvISBN;
    @BindView(R.id.tv_format)
    TextView mTvFormat;
    @BindView(R.id.tv_publisher)
    TextView mTvPublisher;
    @BindView(R.id.tv_read_content)
    TextView mTvReadContent;
    @BindView(R.id.iv_cover)
    ImageView mIvCover;
    @BindView(R.id.iv_tag_periodical)
    ImageView mIvTag;
    private Book mBook;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mBook = bundle.getParcelable(KEY);
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_book_detail;
    }

    @OnClick(R.id.tv_return)
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        pop();
    }

    @Override
    protected void init() {
        if (mBook != null && getActivity() != null) {
            mTvName.setText(String.format("《%s》", mBook.getBookName()));
            mTvAuthor.setText(String.format("%s%s", getString(R.string.text_author), mBook.getAuthor()));
            mTvBinding.setText(String.format("%s%s", getString(R.string.text_book_binding), mBook.getLayout()));

            mTvPublishTime.setText(String.format("%s%s", getString(R.string.text_publish_time), mBook.getPublishingTime()));
            mTvPage.setText(String.format("%s%s", getString(R.string.text_book_page_no), mBook.getPageNumber()));
            mTvISBN.setText(String.format("%s%s", getString(R.string.text_isbn), mBook.getIsbn()));
            mTvFormat.setText(String.format("%s%s", getString(R.string.text_format), mBook.getOpenBook()));
            mTvPublisher.setText(String.format("%s%s", getString(R.string.text_publisher), mBook.getFkPressName()));
            mTvReadContent.setText(mBook.getIntroduction());
            mIvTag.setVisibility("0".equals(mBook.getBop()) ? View.VISIBLE : View.INVISIBLE);
            Glide.with(getActivity())
                    .load(mBook.getCoverPhotoUrl())
                    .centerCrop()
                    .error(R.mipmap.bg_default_cover)
                    .placeholder(R.mipmap.bg_default_cover)
                    .into(mIvCover);
        }
    }
}